#include "login.h"
#include "ui_login.h"
#include "register.h"
#include "findpsd.h"
#include "mainwindow.h"
#include <QMovie>
#include <QPixmap>


Login::Login(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::Login) {
	ui->setupUi(this);
    ui->lineEdit_Name->setAlignment(Qt::AlignHCenter);
    ui->lineEdit_Name->setPlaceholderText("用户名");
    ui->lineEdit_Password->setAlignment(Qt::AlignHCenter);
    ui->lineEdit_Password->setPlaceholderText("密码");
	qDebug() << IP << endl;
	QMovie *movie = new QMovie("F:/1.gif");
	socket = new QTcpSocket(this);
    Init();

	connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
	connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
	        this, SLOT(displayError(QAbstractSocket::SocketError)));  //发生错误时执行displayError函数

    QImage *img=new QImage; //新建一个image对象
    img->load(":/icon/neutral.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
    ui->label_5->setPixmap(QPixmap::fromImage(*img));
}

//QString Login::IP = "2409:8938:c33:f6b:b1cf:7754:a07:acc";
QString Login::IP = " 192.168.43.173";
//192.168.80.66
int Login::Port = 8888;

Login::~Login() {
	delete this->socket;
	delete ui;
}

void Login::Init(){
    //通过本地配置实现记住密码
    QSettings config ("UserConfig.ini",QSettings::IniFormat);
    QString userName = config.value("USERCONFIG/UserName","").toString();
    QString passWord = config.value("USERCONFIG/PassWord","").toString();
    int isRember_now = config.value("USERCONFIG/IsRemeber","0").toInt();
    int isAuto_now = config.value("USERCONFIG/IsAutoLog","0").toInt();
    //
    if(!userName.isEmpty()&&!passWord.isEmpty()){
        ui->lineEdit_Name->setText(userName);
        ui->lineEdit_Password->setText(passWord);
        nameStr = userName;
        passStr = passWord;
    }

    if(isRember_now == 1){
        ui->checkBox_Reme->setChecked(true);
        isRember = true;
    }
    else{
        isRember = false;
    }
    if(isAuto_now == 1){
        ui->checkBox_Auto->setChecked(true);
        isAuto = true;
    }
    else{
        isRember = false;
    }

    connect(ui->checkBox_Reme,SIGNAL(stateChange(int)),this,SLOT(slot_ChangeRem(int)));
    connect(ui->checkBox_Auto,SIGNAL(stateChange(int)),this,SLOT(slot_AutoLog(int)));
}


bool Login::slot_ChangeRem(int state){
    if(state != Qt::Checked){
        isRember = false;
        QSettings config ("UserConfig.ini",QSettings::IniFormat);
        config.value("USERCONFIG/IsRemeber",0);
    }
    return true;
}

bool Login::slot_AutoLog(int state){
    if(state != Qt::Checked)
        return false;
    nameStr = ui->lineEdit_Name->text();
    passStr = ui->lineEdit_Password->text();
    if(nameStr.isEmpty()||passStr.isEmpty()){
       // QMessageBox::warning(this,QString::fromLocal8Bit("错误")，)
        return false;
    }
    else{
        on_pushButton_Register_clicked();
    }
}

void Login::readMsg() {//读取信息
	QString data = socket->readAll();
	QStringList list = data.split("#");
	qDebug() << data;
	if (list[0] == "b" && list[2] == "true") {
        Mainwindow *w = new Mainwindow(list[1]);
        //w->setWindowTitle(list[1]);
		w->show();
	} else if (list[0] == "b" && list[1] == "false" && list[2] == '0')
		QMessageBox::information(this, "信息提示", "登录失败,密码错误!", QMessageBox::Ok);
	else if (list[0] == "b" && list[1] == "false" && list[2] == '2')
		QMessageBox::information(this, "信息提示", "用户已在线，拒绝重复登录!", QMessageBox::Ok);
	else if (list[0] == "b" && list[1] == "false" && list[2] == '3')
		QMessageBox::information(this, "信息提示", "用户不存在，请先注册!", QMessageBox::Ok);
	else
		return;
}


void Login::on_pushButton_Register_clicked() {
	Register *m = new Register;
	m->setWindowTitle("Regist");
	m->show(); //当点击注册账户时，弹出注册窗口
}

void Login::on_pushButton_Forget_clicked() {
	FindPsd *n = new FindPsd;
	n->setWindowTitle("FindPsd");
	n->show();//当点击忘记密码时，显示找回密码界面
}

void Login::on_pushButton_Login_clicked() {
	socket->abort();   //取消已有的连接
	socket->connectToHost(IP, Port); //连接服务器
	qDebug() << IP << endl;

	if (!socket->waitForConnected(30000)) {
		qDebug() << "Connection failed!";
		QMessageBox::information(this, tr("错误"), tr("服务器未开启，请先开启服务器！"));
		ui->lineEdit_Name->clear();  //清空账号栏
		ui->lineEdit_Password->clear();  //清空密码栏
		ui->lineEdit_Name->setFocus();  //将鼠标重新定位到账号栏
		return;
	}

	qDebug() << "Connect successfully!666666!";
	QString userName = ui->lineEdit_Name->text();
	QString passward = ui->lineEdit_Password->text();
	QString bs = "b";
	QString data = bs + "#" + userName + "#" + passward;
	socket->write(data.toUtf8());
	if (userName == NULL) { // 输入为空的提示
		QMessageBox::information(this, tr("错误"), tr("账号不能为空！"));
		ui->lineEdit_Name->setFocus();  //将鼠标重新定位到账号栏
	} else if (passward == NULL) {
		QMessageBox::information(this, tr("错误"), tr("密码不能为空！"));
		ui->lineEdit_Name->setFocus();
	}
	ui->lineEdit_Name->clear();  //清空账号栏
	ui->lineEdit_Password->clear();  //清空密码栏
	ui->lineEdit_Name->setFocus();  //将鼠标重新定位到账号栏
}

void Login::changePicture(){  //输入密码
    QString picName;
        count_password = count_password+1;
        if(count_password>2) count_password=2;
        switch(count_password)
        {
        case 1:picName=":/icon/peek.png";break;
        case 2:picName=":/icon/shy.png";break;
        default:break;
        }
        QImage *img=new QImage;
        img->load(picName);
        ui->label_5->setPixmap(QPixmap::fromImage(*img));
        update();	//这句话还是挺重要的，不加和加有很大的区别
        timer->stop();
}
void Login::changePicture2(){  //密码输入完成
    QString picName;
        count_password = count_password+1;
        if(count_password>3) count_password=3;
        switch(count_password)
        {
        case 2:picName=":/icon/peek.png";break;
        case 3:picName=":/icon/neutral.png";break;
        default:break;
        }
        QImage *img=new QImage;
        img->load(picName);
        ui->label_5->setPixmap(QPixmap::fromImage(*img));
        update();	//这句话还是挺重要的，不加和加有很大的区别
        if(count_password == 3) timer->stop();
}
void Login::on_lineEdit_Password_editingFinished()  //输入密码已完成
{
    flag_password = false; //防止频闪
    QImage *img=new QImage; //新建一个image对象
    img->load(":/icon/peek.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
    ui->label_5->setPixmap(QPixmap::fromImage(*img));
    count_password = 1;
    //timer=new QTimer(this);
    timer->start(30);
    connect(timer,SIGNAL(timeout()),this,SLOT(changePicture2()));//定时器溢出关联changePicture()函数
}



void Login::on_lineEdit_Name_cursorPositionChanged(int arg1, int arg2) //输入用户名
{
    QImage *img=new QImage; //新建一个image对象
    img->load(":/icon/ecstatic.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
    ui->label_5->setPixmap(QPixmap::fromImage(*img));
}


void Login::on_lineEdit_Password_cursorPositionChanged(int arg1, int arg2)   //输入密码
{
    if (flag_password == false){
        QImage *img=new QImage; //新建一个image对象
        img->load(":/icon/neutral.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
        ui->label_5->setPixmap(QPixmap::fromImage(*img));
        count_password = 1;
        timer=new QTimer(this);
        timer->start(30);
        connect(timer,SIGNAL(timeout()),this,SLOT(changePicture()));//定时器溢出关联changePicture()函数
    }
    flag_password = true;
}

void Login::on_lineEdit_Name_editingFinished()
{
    QImage *img=new QImage; //新建一个image对象
    img->load(":/icon/neutral.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
    ui->label_5->setPixmap(QPixmap::fromImage(*img));
}

void Login::on_pushButton_hidden_clicked()
{

    if(hidden_flag){
        //ui->pushButton_hidden->setStyleSheet("QPushButton{background-color: }");
        ui->pushButton_hidden->setStyleSheet("QPushButton{background-image: url(:/icon/view.png);background-color: rgb(0, 0, 0,0);}");
        ui->lineEdit_Password->setEchoMode(QLineEdit::Normal);
        hidden_flag = 0;
        flag_password = false; //防止频闪
        flag_password = false; //防止频闪
        QImage *img=new QImage; //新建一个image对象
        img->load(":/icon/peek.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
        ui->label_5->setPixmap(QPixmap::fromImage(*img));
//        QImage *img=new QImage; //新建一个image对象
//        img->load(":/icon/peek.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径

//        count_password = 1;
//        //timer=new QTimer(this);
//        timer->start(30);
//        connect(timer,SIGNAL(timeout()),this,SLOT(changePicture2()));//定时器溢出关联changePicture()函数

    }
    else{
        ui->pushButton_hidden->setStyleSheet("QPushButton{background-image: url(:/icon/view-off.png);background-color: rgb(0, 0, 0, 0);}");
        //ui->pushButton_hidden->setStyleSheet("QPushButton{}");
        ui->lineEdit_Password->setEchoMode(QLineEdit::Password);
        hidden_flag = 1;
        if (flag_password == false){
            QImage *img=new QImage; //新建一个image对象
            img->load(":/icon/neutral.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
            ui->label_5->setPixmap(QPixmap::fromImage(*img));
            count_password = 1;
            timer=new QTimer(this);
            timer->start(30);
            connect(timer,SIGNAL(timeout()),this,SLOT(changePicture()));//定时器溢出关联changePicture()函数

        }
        flag_password = true;
    }
}
